package thread.threaddemo;

/**
 * @Author: wenjingyuan
 * @Date: 2022/11/09/8:53
 * @Description:使用自定义标识符的方式来终止线程
 */
public class ThreadByInterrupt {
    private volatile static  boolean flag=false;
    public static void main(String[] args) throws InterruptedException {
        Thread thread=new Thread(()->{
            while(!flag){
                System.out.println("正在转账...");
                try {
                    Thread.sleep(800);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("还好，差点误了大事");
        });
        thread.start();

        Thread.sleep(1000);
        //终止线程
        System.out.println("有内鬼，终止交易");
        flag=true;
    }
}
